package com.lagou.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.common.Const;
import com.lagou.common.ResponseCode;
import com.lagou.common.ServerResponse;
import com.lagou.mapper.UserMapper;
import com.lagou.model.User;
import com.lagou.service.IUserService;
import com.lagou.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public ServerResponse<PageInfo> getUsers(int pageNumb, int pageSize) {
        PageHelper.startPage(pageNumb,pageSize);
        List<User> userList= userMapper.userList();
        PageInfo pageInfo = new PageInfo(userList);
        return ServerResponse.createBySuccessData(pageInfo);
    }

    @Override
    public ServerResponse<User> login(String username, String password) {
        int resultNumb = userMapper.checkUser(username);
        if(resultNumb == 0){
            return ServerResponse.createByErrorMsg("该用户不存在");
        }

        String md5Pass = MD5Util.MD5EncodeUtf8(password);
        User user = userMapper.selectLogin(username, md5Pass);
        if(user == null){
            return ServerResponse.createByErrorMsg("密码不正确");
        }
        user.setPassword(StringUtils.EMPTY);
        return ServerResponse.createBySuccessMsgAndData("登录成功",user);
    }

    @Override
    public ServerResponse<String> add(User user) {
        ServerResponse<String> validResponse = this.checkValid(user.getUsername(), Const.USERNAME);
        if(validResponse.getCode() != ResponseCode.SUCCESS.getCode()){
            return validResponse;
        }
        validResponse = this.checkValid(user.getEmail(),Const.EMAIL);
        if(validResponse.getCode() != ResponseCode.SUCCESS.getCode()){
            return validResponse;
        }
        String md5Pass = MD5Util.MD5EncodeUtf8(user.getPassword());
        user.setPassword(md5Pass);
        int insert = userMapper.insert(user);
        if(insert == 0){
            return ServerResponse.createBySuccessMsg("添加失败");
        }
        return ServerResponse.createBySuccessMsg("添加成功");
    }

    @Override
    public ServerResponse<String> deleteById(int id) {
        //1.判断该用户是否存在,获取用户信息
        ServerResponse<User> information = this.selectById(id);
        User user = information.getData();
        if(user ==null){
            //2.如果用户不存在,返回失败的ServerResources
            return ServerResponse.createByErrorMsg("删除的用户不存在");
        }
        //3.如果用户存在,调用mapper 中的方法进行删除 ,得到返回值  count ==1 删除成功  count==0 ,删除失败
        int count = userMapper.deleteById(id);
        if(count > 0){
            return ServerResponse.createBySuccessMsg("删除成功");
        }

        return ServerResponse.createByErrorMsg("删除失败");
    }

    public ServerResponse<User> selectById(int id){
        User user =  userMapper.selectById(id);
        //根据id查到了用户
        if(user != null){
            return ServerResponse.createBySuccessData(user);
        }
        return ServerResponse.createByErrorMsg("用户不存在");
    }

    public ServerResponse<String> checkValid(String str,String type){
        if(StringUtils.isNotBlank(type)){
            if(Const.USERNAME.equals(type)){
                int resultCount = userMapper.checkUser(str);
                if(resultCount > 0){
                    return ServerResponse.createByErrorMsg("用户已存在");
                }
            }

            if(Const.EMAIL.equals(type)){
                int emailCount = userMapper.checkEmail(str);
                if(emailCount > 0){
                    return ServerResponse.createByErrorMsg("邮箱已存在");
                }
            }
        }
        return ServerResponse.createBySuccessMsg("校验成功");
    }
}
